package org.qpcr.community.project.web.excel;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
import org.hswebframework.reactor.excel.CellDataType;
import org.hswebframework.reactor.excel.ExcelHeader;
import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.bean.FastBeanCopier;
import org.hswebframework.web.validator.ValidatorUtils;
import org.qpcr.community.project.model.entity.TemperatureControlEntity;
import org.qpcr.core.metadata.Jsonable;

import javax.persistence.Column;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Getter
@Setter
public class TemperatureControlExcelInfo implements Jsonable {

    @org.qpcr.community.io.excel.annotation.ExcelHeader(value = "编码")
    @NotBlank(message = "编码不能为空")
    private String code;

    @org.qpcr.community.io.excel.annotation.ExcelHeader(value = "算法值")
    @NotBlank(message = "算法值不能为空")
    private String algorithm;
    @org.qpcr.community.io.excel.annotation.ExcelHeader(value = "算法值英文")
    @NotBlank(message = "算法值英文不能为空")
    private String algorithmEn;


    private TemperatureControlEntity temperatureControlInstance;

    private Map<String, Object> configuration = new HashMap<>();

    private long rowNumber;

    private String state;

    public Map<String, Object> toMap() {
        return FastBeanCopier.copy(this, new HashMap<>());
    }

    public static List<ExcelHeader> getTemplateHeaderMapping(TemperatureControlExcelFilterColumns filterColumns) {
        return Arrays.stream(new ExcelHeader[]{
                         new ExcelHeader("code", "编码", CellDataType.STRING),
                         new ExcelHeader("algorithm", "算法值", CellDataType.STRING),
                         new ExcelHeader("algorithmEn", "算法值英文", CellDataType.STRING)
                     })
                     .filter(a -> !filterColumns.getColumns().contains(a.getKey()))
                     .collect(Collectors.toList());
    }

    public static List<ExcelHeader> getExportHeaderMapping(TemperatureControlExcelFilterColumns filterColumns) {
        return Arrays.stream(new ExcelHeader[]{
                         new ExcelHeader("code", "编码", CellDataType.STRING),
                         new ExcelHeader("algorithm", "算法值", CellDataType.STRING),
                         new ExcelHeader("algorithmEn", "算法值英文", CellDataType.STRING)
                     })
                     .filter(a -> !filterColumns.getColumns().contains(a.getKey()))
                     .collect(Collectors.toList());
    }

    /**
     * 初始化保存实体
     *
     * @param temperatureControlInstanceListDb 数据库已存在的数据
     * @param auth                             认证数据
     * @return TemperatureControlExcelInfo
     */
    public TemperatureControlExcelInfo initTemperatureControlInstance(List<TemperatureControlEntity> temperatureControlInstanceListDb, Authentication auth) {
        List<TemperatureControlEntity> list = temperatureControlInstanceListDb.stream()
                                                                              .filter(db -> db.getCode() != null && db
                                                                                  .getCode()
                                                                                  .equals(this.getCode()))
                                                                              .collect(Collectors.toList());
        if (list.size() > 0) {
            throw new RuntimeException("编号已存在");
        }
        TemperatureControlEntity entity = FastBeanCopier.copy(this, new TemperatureControlEntity());

        entity.setCreateTimeNow();
        entity.setCreatorId(auth.getUser().getId());
        entity.setCreatorName(auth.getUser().getName());

        entity.setModifyTimeNow();
        entity.setModifierId(auth.getUser().getId());
        entity.setModifierName(auth.getUser().getName());

        ValidatorUtils.tryValidate(entity);

        this.temperatureControlInstance = entity;
        return this;
    }

    @Override
    public void fromJson(JSONObject json) {
        Jsonable.super.fromJson(json);
    }
}
